package com.crm.crm.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crm.crm.domain.BusinessOpportunity;
import com.crm.crm.domain.dto.report.CountShipDto;
import com.crm.crm.mapper.ClueProcessMapper;
import com.crm.crm.service.ClueProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.crm.crm.domain.ClueProcess;

import static com.baomidou.mybatisplus.extension.toolkit.Db.updateById;

@Service
//客户流程表的增删改查方法
public class ClueProcessServiceImpl implements ClueProcessService {
    @Autowired
    private ClueProcessMapper clueProcessMapper;
    @Autowired
    private DeptServicelmpl deptServicelmpl;
    @Autowired
    private UserServicelmpl userServicelmpl;
    @Autowired
    private ClueServiceImpl clueServiceImpl;
    @Autowired
    private BusinessServicelmpl businessServiceImpl;

    /*线索统计的分页查询*/
    @Override
    public Page<ClueProcess> list(CountShipDto dto, Page<ClueProcess> page) {
        LambdaQueryWrapper<ClueProcess> wrapper = new LambdaQueryWrapper<>();

        // 根据起始时间查询。判断是否有内容，没有则查询所有
        // 根据起始时间查询。判断是否有内容，没有则查询所有
        // 时间范围查询
            wrapper.ge(ClueProcess::getClueCreateTime, dto.getBeginCreateTime());
            wrapper.le(ClueProcess::getClueCreateTime, dto.getEndCreateTime() + " 23:59:59");
        // 使用 MyBatis Plus 分页查询
        return clueProcessMapper.selectPage(page, wrapper);
    }












    //    ======当修改商机数据时候，只修改商机状态 todo
//    缺少客户几个表关联的唯一id，所以，通过电话号码+名字来保证，唯一性
//
//    public void updateBusinessStatus(BusinessOpportunity b) {
//        ClueProcess p = new ClueProcess();
////        拷贝数据
//        BeanUtils.copyProperties(b,p);
//        p.setBusinessStatus(b.getStatus());
////        先通过电话号码+名字来查询出对应的数据
//        ClueProcess process = select(p.getPhone(), p.getName());
////        如果数据是空的，啦就插入数据
////        如果有数据。那就修改数据
//        if (process == null) {
////            没有数据，查询商机表电话和名字对应的数据
//            BusinessOpportunity businessOpportunity = businessServiceImpl.selectById(b.getId());
//            add(p);
//        }
//        process.setBusinessStatus(p.getBusinessStatus());
////        如果所属人不为空，则设置所属人为当前登录用户，并且查询部门id并修改
//        if (StringUtils.isNotEmpty(p.getOwner())) {
//            process.setOwner(p.getOwner());
//            userServicelmpl.getUserById(p.getOwner());
//            process.setDeptId(userServicelmpl.getUserById(p.getOwner()).getDeptId());
//        }
//       updateById(process);
//    }

    //====工具方法==========================
//通过电话号+名字来保证唯一性，然后查询出对应的客户数据
    private ClueProcess select(String photo, String name) {
        return clueProcessMapper.select(photo, name);
    }

    //    增
    public int add(ClueProcess process) {
        return clueProcessMapper.insert(process);
    }

    //    删
    public int delete(Integer id) {
        return clueProcessMapper.deleteById(id);
    }

    //    改
    public int update(ClueProcess process) {
        return clueProcessMapper.updateById(process);
    }

    //    查
    public ClueProcess select(Integer id) {
        return clueProcessMapper.selectById(id);
    }


}
